(function () {
	 /**
	 * 异步json获取
	 * @param {String} url 接口地址
	 * @param {Object} param 发送数据
	 * @param {Function} [success] 成功回调
	 * @param {Function} [error] 错误回调
	 */
var $modal = $('#pop-modal');
	 
	 getPageData({});
	 
	 function getPageData (Filter)
	 {
	 	var Filter = Filter;
	 		Filter.page = page.page;
	 		Filter.pageSize = page.pageSize;
	 	getJson(Domain+'/ec/goods/doSearchGoodsByProps.do', Filter, function (res) {
		  if(!res.success){
	
		    $('.am-modal-bd').text(res.errorMessage);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		  }
		  else{

	
	  	 	 $('#totalnum').text(res.data.rowCount);	

	  		  page.nowPage = res.data.pageId;
      		  page.pageCount = res.data.pageCount;
      		  
      		  if(!res.data.rows.length){
		        $('.am-pagination').remove();
		        $('.list-body').append('<li class="empty_div">没有相关的商品哦~</li>');
		        return
		      }

		     $('.list-body').empty();

			 renderTmp('#carlist', 'carlisttpl',res);
			 	lazyLoad.refresh(true);
			 	$('.am-pagination').createPage({
			        current: page.page,
			        type: 1,
			        pageCount: page.pageCount,
			        center: 3,
			        showLen: 11,
		        callback: function (curidx, obj) {
		          page.page = curidx;
		          $('#carlist').empty();
		          getPageData(Filter);
		        }
		       });

			 	$('.textpage').createPage({
			        current: page.page,
			        type: 2,
			        pageCount: page.pageCount,
		        callback: function (curidx, obj) {
		          page.page = curidx;
		          $('#carlist').empty();
		          getPageData(Filter);
		        }
		       });
		  }
		})
	 }

	 //综合排序
	 $('.normal').on('click',function () {

	 	getPageData({});

	 	activeOrder ($(this));

	 })

	 //价格排序
	 $('.orderprice').on('click',function () {

	 	if($(this).hasClass('sheng'))
	 	{
	 		$(this).addClass('jiang').removeClass('sheng');
	 		getPageData({
	 			sort:'a'
			});
	 	}
	 	else
	 	{
	 		$(this).addClass('sheng').removeClass('jiang');
	 		getPageData({
	    		sort:'b'
			});
	 	}

	 	activeOrder ($(this));

	 });

	 //年份排序
	 $('.orderyear').on('click',function () {

	 	if($(this).hasClass('sheng'))
	 	{
	 		$(this).addClass('jiang').removeClass('sheng');
	 		getPageData({
	    		ye:'a'
			});
	 	}
	 	else
	 	{
	 		$(this).addClass('sheng').removeClass('jiang');
	 		getPageData({
	    		ye:'b'
			});
	 	}

	 	activeOrder ($(this));

	 });

	 //人气排序
	 $('.orderpeople').on('click',function () {

	 	if($(this).hasClass('sheng'))
	 	{
	 		$(this).addClass('jiang').removeClass('sheng');
	 		getPageData({
	    		ht:'a'
			});
	 	}
	 	else
	 	{
	 		$(this).addClass('sheng').removeClass('jiang');
	 		getPageData({
	    		ht:'b'
			});
	 	}

	 	activeOrder ($(this));

	 });

	 //排序样式激活
	 function activeOrder (obj)
	 {
	 	$('.sort').find('span').not('.title').removeClass('active');
	 	$(obj).addClass('active');
	 }

	 //点击收藏&&取消收藏
	$('#carlist').delegate('.btn-cang','click',function (e) {

		var iGoodsId = $(this).attr('data-id');
		
			if($(this).hasClass('favot'))
			{

				$.ajax({
				    type: 'POST',
				    url:  Domain+'/mbr/doRemoveGoodsFromFav.do',
				    data: {id:iGoodsId},
				    dataType: 'json',
				    success: function (res) {
				    	
				    	$('.am-modal-bd').text(language.cangCancel);
						$modal.modal();
						setTimeout(function () {
							$modal.modal('close');
						},iWiating);
				    	$(this).removeClass('favot');
				    },
				    error:function(msg){

				    		$('.am-modal-bd').text(language.ajaxFailed);
							$modal.modal();
						setTimeout(function () {
							$modal.modal('close');
						},iWiating);

				    }
				  });

				return;
			}

		 	$.ajax({
		    type: 'POST',
		    url:  Domain+'/mbr/doAddGoodsToFav.do',
		    data: {id:iGoodsId},
		    dataType: 'json',
		    success: function (res) {
		    	
		    	if(res.errorMessage)
		    	{
		    		$('.am-modal-bd').text(res.errorMessage);
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);
					return;
		    	}

		    	$('.am-modal-bd').text(language.cangSuc);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);
		    	$(this).addClass('favot');
		    },
		    error:function(msg){

		    		$('.am-modal-bd').text(language.ajaxFailed);
					$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		    }
		  });

	 	e.preventDefault();
	 
	});

	//添加对比
	$('#carlist').delegate('.btn-compare','click',function (e) {

		var iGoodsIds = cookie.get('goods_ids');
		var iGoodsId = $(this).attr('data-id');
		e.preventDefault();
		if(iGoodsIds)
		{
			iGoodsIds = iGoodsIds.split('-');
			
			if(iGoodsIds.indexOf(iGoodsId)==-1)
			{
				iGoodsIds.push(iGoodsId);
				cookie.set('goods_ids',iGoodsIds.join('-'),iCookieDays)
				$('.am-modal-bd').text(language.pkSuc);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);
			}
			else
			{
				$('.am-modal-bd').text(language.pkAlready);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);
			}
			
		}
		else
		{
			cookie.set('goods_ids',iGoodsId,iCookieDays);
			$('.am-modal-bd').text(language.pkSuc);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);
		}

	});

	//删除代理
	$('#carlist').delegate('.btn-del','click',function (e) {

		var iGoodsId = $(this).attr('data-id');
			
			$.ajax({
		    type: 'POST',
		    url:  Domain+'/ec/usedAgent/doCancelAgent.do',
		    data: {goodsId:iGoodsId},
		    dataType: 'json',
		    success: function (res) {
		    	
		    	if(res.errorMessage)
		    	{
		    		$('.am-modal-bd').text(res.errorMessage);
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);
					return;
		    	}

		    	$('.am-modal-bd').text(language.qxAgent);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);
		    },
		    error:function(msg){

		    		$('.am-modal-bd').text(language.ajaxFailed);
					$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		    }
		  });
		
		e.preventDefault();
	});

	//添加代理
	$('#carlist').delegate('.btn-sure','click',function (e) {

		var iGoodsId = $(this).attr('data-id');

		$.ajax({
		    type: 'POST',
		    url:  Domain+'/ec/usedAgent/doAgent.do',
		    data: {goodsId:iGoodsId},
		    dataType: 'json',
		    success: function (res) {
		    	
		    	if(res.errorMessage)
		    	{
		    		$('.am-modal-bd').text(res.errorMessage);
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);
					return;
					
		    	}

		    	$('.am-modal-bd').text(language.qdAgent);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		    },
		    error:function(msg){

		    		$('.am-modal-bd').text(language.ajaxFailed);
					$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		    }
		  });
		
		e.preventDefault();
	});



})();